package com.erclab.internal.xpresso.webapp.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.*;

import com.erclab.internal.xpresso.webapp.WebObjectFactory;

/**
 * @version 	1.0
 * @author
 */
public class FormConstructor extends HttpServlet {

	/**
	* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		doPost(req, resp);
	}

	/**
	* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException, SecurityException {
		
			String viewHandler;
			try {
				viewHandler = WebObjectFactory.createAndAddForm(request, extractFormName(request.getServletPath()));
			} catch (SecurityException e){
				throw e;			
			} catch (Exception e) {
				// TODO log4j
				throw new ServletException(e);
			}
			
		RequestDispatcher view = request.getRequestDispatcher(viewHandler);
		view.forward(request, response);	
	}

	/**
	 * @param string
	 * @return
	 */
	protected String extractFormName(String path) {
		return path.substring(path.lastIndexOf('/') + 1, path.indexOf(".form"));
	}

}
